package com.zuoyy.modules.system.service.impl;

import com.zuoyy.common.enums.StatusEnum;
import com.zuoyy.common.utils.FileUtil;
import com.zuoyy.modules.common.BaseServiceImpl;
import com.zuoyy.modules.system.domain.SysFile;
import com.zuoyy.modules.system.query.FileQuery;
import com.zuoyy.modules.system.repository.FileRepository;
import com.zuoyy.modules.system.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 
 */
@Service
public class FileServiceImpl extends BaseServiceImpl<SysFile> implements FileService {

    @Autowired
    private FileRepository fileRepository;



    @Override
    public Page<SysFile> getPageList(FileQuery query) {
        // 创建分页对象
        Pageable pageable = PageRequest.of(query.getPageIndex(), query.getPageSize(), new Sort(query.getSort(),query.getColumn()));
        return fileRepository.findAll(query,pageable);
    }

    @Override
    public List<SysFile> findByQuery(FileQuery query) {
        return fileRepository.findAll(query);
    }

    @Override
    public List<SysFile> findByQuery(FileQuery query, Sort sort) {
        return fileRepository.findAll(query,sort);
    }

    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<String> idList) {
        return fileRepository.updateStatus(statusEnum.code,idList) > 0;
    }

    @Override
    public Boolean delByBusinessId(String businessId) {
        FileQuery query = new FileQuery();
        query.setBusinessId(businessId);
        List<SysFile> files = fileRepository.findAll(query);
        if(files!=null&&files.size()>0){
            for(SysFile file : files){
                try {
                    FileUtil.delFile(file.getPath());
                }catch (Exception e){
                }
            }
        }
        return fileRepository.delByBusinessId(StatusEnum.DELETE.code,businessId)>0;
    }
}